/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.business;

import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.plaf.DefaultLookAndFeel;

/**
 *
 * @author Sun
 */
public class CustomLookAndFeel extends DefaultLookAndFeel {

    private ScrollBarAnimation scrollAnimation;

    public void drawVerticalScroll(Graphics g, Component c, float offsetRatio, float blockSizeRatio) {
        if (verticalScroll == null) {
            initScroll();
        }
        if (scrollAnimation == null || scrollAnimation.getComponent() != c) {
            scrollAnimation = new ScrollBarAnimation(c);
            scrollAnimation.getUnselectedStyle().setBgColor(0xffffff);
        }

        int borderW = 0;
        if (c.getStyle().getBorder() != null) {
            borderW = c.getStyle().getBorder().getThickness();
        }
        int x = c.getX();
        if (!c.isRTL()) {
            x += c.getWidth() - getVerticalScrollWidth() - borderW;
        } else {
            x += borderW;
        }
        int y = c.getY();
        int height = c.getHeight();
        int width = getVerticalScrollWidth();
        if (!scrollAnimation.isPainted()) {
            scrollAnimation.setPainted(true);
            scrollAnimation.setTime(System.currentTimeMillis());
            drawScroll(g, c, offsetRatio, blockSizeRatio, true, x, y, width, height, verticalScroll, scrollAnimation);
            scrollAnimation.registerAnimated();
        } else {
            scrollAnimation.deregisterAnimated();
            scrollAnimation.setPainted(false);
            scrollAnimation.setTime(0);
        }
    }

    protected void initScroll() {
        super.initScroll();
        verticalScrollThumb = null;
    }



    private class ScrollBarAnimation extends Component {

        private Form parent;
        private Component cmp;
        private long time;
        private boolean painted;
        private static final int DELAY = 750;
        public ScrollBarAnimation(Component cmp) {
            this.cmp = cmp;
            this.parent = cmp.getComponentForm();
        }

        public String getUIID() {
            return "ScrollThumb";
        }

        public void registerAnimated() {
            parent.registerAnimated(this);
        }

        public void deregisterAnimated() {
            parent.deregisterAnimated(this);
        }

        public Component getComponent() {
            return cmp;
        }

        public boolean isPainted() {
            return painted;
        }

        public void setPainted(boolean painted) {
            this.painted = painted;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public boolean animate() {
            if (!parent.isVisible()) {
                parent.deregisterAnimated(this);
                return false;
            }

            if (painted && time != 0) {
                if (System.currentTimeMillis() - time >= DELAY) {
                    cmp.repaint();
                }
                return false;
            }
            deregisterAnimated();
            return false;
        }
    }
}
